package com.delivery.deliverysystem.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.delivery.deliverysystem.common.ServerResponse;
import com.delivery.deliverysystem.exception.RException;
import com.delivery.deliverysystem.pojo.DAO.UserDTO;
import com.delivery.deliverysystem.pojo.DO.SystemLog;
import com.delivery.deliverysystem.pojo.VO.UserVO;
import com.delivery.deliverysystem.service.SystemLogService;
import com.delivery.deliverysystem.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户控制类
 */
@RestController
@RequestMapping("/sysUser")
@Api(tags = "用户接口")
public class SysUserController {

    @Autowired
    private UserService userService;
    @Autowired
    private SystemLogService systemLogService;

    @PostMapping("/createUser")
    @ApiOperation(value = "创建用户",response = ServerResponse.class,httpMethod = "POST")
    public ServerResponse createUser(@RequestBody UserDTO userDTO){
        return userService.createUser(userDTO);
    }

    @PostMapping("/updateUser")
    @ApiOperation(value = "修改用户",response = ServerResponse.class,httpMethod = "POST")
    public ServerResponse updateUser(@RequestBody UserDTO userDTO) {
        try {
            boolean b = userService.updateUser(userDTO);
            if (b) return ServerResponse.createBySuccess("修改成功");
            return ServerResponse.createByError("修改失败");
        } catch (RException re) {
            return ServerResponse.createByError(re.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return ServerResponse.createByError("修改失败");
        }
    }

    @DeleteMapping("/deleteUser")
    @ApiOperation(value = "删除用户",response = ServerResponse.class,httpMethod = "DELETE")
    public ServerResponse deleteUser(@RequestParam Integer id){
        //不清楚要不要逻辑删除，所以先用物理删除
        boolean b = userService.removeById(id);
        if(b) return ServerResponse.createBySuccess("删除成功");
        return ServerResponse.createByError("删除失败");
    }

    @GetMapping("/getUserById")
    @ApiOperation(value = "根据id查询用户",response = ServerResponse.class,httpMethod = "GET")
    public ServerResponse getUserById(@RequestParam Integer id){
        UserVO userById = userService.getUserById(id);
        if(userById==null) return ServerResponse.createByError("用户不存在");
        return ServerResponse.createBySuccess(userById);
    }

    @GetMapping("/getUserList")
    @ApiOperation(value = "获取用户列表",response = ServerResponse.class,httpMethod = "GET")
    public ServerResponse getUserList(){
        List<UserVO> userList = userService.getUserList();
        if(userList!=null && !userList.isEmpty()) return ServerResponse.createBySuccess(userList);
        return ServerResponse.createBySuccess("用户列表为空");
    }
    //用户登录
    @PostMapping("/login")
    @ApiOperation(value = "用户登录", response = ServerResponse.class, httpMethod = "POST")
    @Transactional(rollbackFor = {Exception.class, RuntimeException.class, RException.class})
    public ServerResponse login(@RequestParam String username, @RequestParam String password) throws Exception {
        return userService.login(username, password);
    }
    //用户退出
    @PostMapping("/logout")
    @ApiOperation(value = "用户退出", response = ServerResponse.class, httpMethod = "POST")
    @Transactional(rollbackFor = {Exception.class, RuntimeException.class, RException.class})
    public ServerResponse logout(@RequestParam String username) throws Exception {
        return userService.logout(username);
    }

    @GetMapping("/getLogList")
    @ApiOperation(value = "分页获取日志列表", response = ServerResponse.class, httpMethod = "GET")
    public ServerResponse getLogList(
            @RequestParam(defaultValue = "1") long pageNum,
            @RequestParam(defaultValue = "10") long pageSize) {

        IPage<SystemLog> page = systemLogService.getLogList(pageNum, pageSize);
        if (page != null && page.getRecords() != null && !page.getRecords().isEmpty()) {
            return ServerResponse.createBySuccess(page);
        }
        return ServerResponse.createBySuccess("日志信息为空");
    }

}
